/*
 * Copyright (c) 2020 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.andkulikov.transitionseverywhere.slice;

import com.andkulikov.transitionseverywhere.ResourceTable;
import com.andkulikov.transitionseverywhere.slice.util.ResUtil;
import com.transitionseverywhere.*;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.colors.RgbColor;
import ohos.agp.components.*;
import ohos.agp.components.element.ShapeElement;
import ohos.agp.utils.Color;
import ohos.agp.utils.Rect;
import ohos.agp.window.service.Display;
import ohos.agp.window.service.DisplayManager;
import ohos.utils.LightweightMap;

import java.util.ArrayList;

public class ExplodeAndEpicenterSlice extends AbilitySlice implements Component.ClickedListener {
    private ComponentContainer mRootId;
    private TableLayout mTlGridContainer;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        LayoutScatter layoutScatter = LayoutScatter.getInstance(this);
        ComponentContainer root_layout = (ComponentContainer) layoutScatter.parse(ResourceTable.Layout_slice_explode_epicenter, null, false);
        mRootId = (ComponentContainer) root_layout.findComponentById(ResourceTable.Id_parentid);
        super.setUIContent(root_layout);
        mTlGridContainer = (TableLayout) root_layout.findComponentById(ResourceTable.Id_tlGridContainer);
        Display displayMetrics = DisplayManager.getInstance().getDefaultDisplay(getContext()).get();
        int w = displayMetrics.getAttributes().width;
        int h = displayMetrics.getAttributes().height;
        int tot = (w*h)/(220*220);
        int gridcolumn =  w/220;
        int gridrow =  h/220;
        mTlGridContainer.setColumnCount(gridcolumn);
        mTlGridContainer.setRowCount(gridrow);
        Object tagMap = new LightweightMap<String, ArrayList<Transition>>();
        mTlGridContainer.setTag(tagMap);
        createViews(layoutScatter, mTlGridContainer,tot);
    }

    private void createViews(LayoutScatter inflater, ComponentContainer layout, int tot) {
        layout.removeAllComponents();
        for (int i = 0; i < tot; i++) {
            Text btnTap = new Text(this);
            ShapeElement background_color_element = new ShapeElement();
            background_color_element.setShape(ShapeElement.RECTANGLE);
            background_color_element.setRgbColor(new RgbColor(ResUtil.getColor(this, ResourceTable.Color_red)));
            background_color_element.setBounds(0, 0, btnTap.getRight(), btnTap.getBottom());
            btnTap.setBackground(background_color_element);
            ComponentContainer.LayoutConfig layoutConfig = new ComponentContainer.LayoutConfig(200, 200);
            layoutConfig.setMargins(20, 20, 20, 20);
            btnTap.setLayoutConfig(layoutConfig);
            btnTap.setClickedListener(this::onClick);
            btnTap.setText("Tap Me");
            btnTap.setTextSize(45);
            btnTap.setTextColor(new Color(ResUtil.getColor(getContext(), ResourceTable.Color_white)));
            Object tagMap = new LightweightMap<String, int[]>();
            btnTap.setTag(tagMap);
            layout.addComponent(btnTap);
        }
    }

    @Override
    public void onClick(Component component) {
        final Rect viewRect = new Rect();
        component.getSelfVisibleRect(viewRect);
        TransitionSet set = new TransitionSet()
                .addTransition(new Explode().setEpicenterCallback(new Transition.EpicenterCallback() {
                    @Override
                    public Rect onGetEpicenter(Transition transition) {
                        return viewRect;
                    }
                }).excludeTarget(component, true))
                .addTransition(new Fade().addTarget(component))
                .addListener(new Transition.TransitionListenerAdapter() {
                    @Override
                    public void onTransitionEnd(Transition transition) {
                        transition.removeListener(this);
                        onBackPressed();
                    }
                }).setDuration(2000);
        TransitionManager.beginDelayedTransition(mTlGridContainer, set);
    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }
}